///////////////////////////////////////////////////////////////////////////////
//
//
//
///////////////////////////////////////////////////////////////////////////////

package model
{
    import flash.events.ErrorEvent;
    import flash.events.Event;
    import flash.events.IEventDispatcher;

    import model.service.IElfinService;

    import model.vo.ConfigVO;

    import mx.utils.ArrayUtil;

    import org.kolonitsky.alexey.debug.DebugWrapper;
    import org.kolonitsky.alexey.net.ElfinSerivce;

    import org.puremvc.as3.patterns.proxy.Proxy;

    public class ElfinSafeProxy extends Proxy
    {
        public function ElfinSafeProxy(name:String, data:*, config:ConfigVO)
        {
            super(name,  data);
            this.config = config;
            log("Created");
        }

        //---------------------------------------------------------------------
        //
        //  SafeProxy API
        //
        //---------------------------------------------------------------------

        /**
         * Create any service inhetided ElfinService and set default parameters.
         *
         * @param serviceClass
         * @param resultHandler
         * @param faultHandler
         */
        protected function createElfinService(serviceClass:Class,
            resultHandler:Function, faultHandler:Function = null):IElfinService
        {
            var service:* = new serviceClass();

            var elfinService:IElfinService = service as IElfinService;
            if (elfinService)
            {
                elfinService.init(config.baseUrl, config.authToken);
            }
            else
            {
                DebugWrapper.trace("ERROR: ElfinSafeProxy. serviceClass must implement IElfinService" + service)
            }

            var dispatcher:IEventDispatcher = service as IEventDispatcher
            if (dispatcher)
            {
                dispatcher.addEventListener(Event.COMPLETE, resultHandler);
                dispatcher.addEventListener(ErrorEvent.ERROR, faultHandler || errorHandler);
            }
            else
            {
                DebugWrapper.trace("ERROR: ElfinSafeProxy. serviceClass must implement IEventDispatcher" + dispatcher)
            }
            return service;
        }

        /**
         *
         * @param message
         * @param code 0..3
         */
        protected function log(message:*,  code:int=0):void
        {
            if (message is String)
            {
                var prefix:String = ["DEBUG", "INFO", "WARNING", "ERROR"][code];
                message = prefix + ": " + getProxyName() + ": " + message;
            }
            DebugWrapper.trace(message);
        }

        /**
         * Verification function check config availability.
         * @return integer with error code.
         */
        protected function checkConfig():int
        {
            if (config == null)
            {
                log("Application configuration unavailable on access.");
                return 1;
            }

            if (config.authToken.length == 0)
            {
                log("Application not authorized.")
                return 2;
            }

            if (config.baseUrl.length == 0)
            {
                log("Base URL not specified");
                return 3;
            }

            return 0;
        }



        //---------------------------------------------------------------------
        //
        //  Private logic
        //
        //---------------------------------------------------------------------

        /**
         * Application configuration property.
         */
        private var config:ConfigVO;

        /**
         * Default error handler. This function assigned to service
         * ErrorEvent.ERROR event as event handler if custom event handler
         * not defined.
         *
         * @param event
         */
        private function errorHandler(event:ErrorEvent):void
        {
            log(event.text, 2);
        }
    }
}
